var myFunc = require("myCommonFunction.js");


// myFunc.openMyApp("京东");

// myFunc.clickByText('我的');

// myFunc.textCpClick('商品收藏', 5000);
// sleep(2000)
// myFunc.clickByText('贵州茅台');

// 

importClass(android.net.Uri)
var id = '100012043978';
var jd_url = "openapp.jdmobile://virtual?params=%7B%22sourceValue%22:%220_productDetail_97%22,%22des%22:%22productDetail%22,%22skuId%22:%22"+id+"%22,%22category%22:%22jump%22,%22sourceType%22:%22PCUBE_CHANNEL%22%7D";
//两种url拼接都可以
//String jd_url = "openApp.jdMobile://virtual?params={\"category\":\"jump\",\"des\":\"productDetail\",\"skuId\":\""+id+"\",\"sourceType\":\"JSHOP_SOURCE_TYPE\",\"sourceValue\":\"JSHOP_SOURCE_VALUE\"}";
var uri = Uri.parse(jd_url);

app.startActivity({
    action: "android.intent.action.VIEW",
    data:uri,
    flag:'Intent.FLAG_ACTIVITY_NEW_TASK'
});

text('收藏').findOne(5000);
var now = new Date();
var hour = now.getHours();
var min = now.getMinutes();
var sec = now.getSeconds()

if (hour == 10) {
    if(!myFunc.clickByText('立即预约')){
        click(933, 2204)
    }
} else if (hour == 11) {
    var time = (60 - min) * 60 - sec - 15;
    sleep(time * 1000)
    app.startActivity({
        action: "android.intent.action.VIEW",
        data:uri,
        flag:'Intent.FLAG_ACTIVITY_NEW_TASK'
    });
    
    text('收藏').findOne(5000);
    myFunc.log('开始')
    text('开始抢购').findOne().click();
    text('提交订单').findOne().click();
} else {
    
    
    var t = textContains('至').findOne(5000);
    var thread = null;
    if (t != null) {
        var ti = t.text().split(" ")[1];
        log(ti)
        ti = ti.split(":");
        var h = 24;
        if (ti[0] != '00') {
            h = parseInt(ti[0])
        }
        var m = 60;
        if (ti[1] != '00') {
            m = parseInt(ti[1])
        }

      
        var time = (m - min) * 60 - sec - 15;
        log(ti, h, m, time)
        thread = threads.start(function () {
          
            sleep(time * 1000)
            myFunc.log('开始')
            log('开始抢购', text('开始抢购').findOne().click());
            log('提交订单', text('提交订单').findOne().click());
        });
    }

    var w = floaty.window(
        <vertical>
            <text id="timet" text='' />
            {/* <button id="waitq" text="等待抢购" /> */}
            <button id="mashang" text="立即抢购" />

        </vertical>
    );
    w.timet.setText('预估抢购时间为：' + ti + '，如不对手动点击抢购按钮')

    w.setSize(1000, 600);
    // w.setTouchable(true);
    w.setAdjustEnabled(true);
    w.setPosition(50, 200);
    w.exitOnClose();
    setInterval(() => { }, 1000);

    w.mashang.click(function () {
        if(thread != null) {
            thread.interrupt();
        }
        threads.start(function () {
            myFunc.log('开始')
            text('开始抢购').findOne().click();
            text('提交订单').findOne().click();
        });
    });
}

// var r = http.get("https://api.m.jd.com/client.action?functionId=queryMaterialProducts&client=wh5", {
//     headers: {
//         'Accept-Language': 'zh-cn,zh;q=0.5',
//         'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36'
//     }
// });

// log(r.body.json().currentTime2)
